import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

public class HorizontalScrollBar extends GuiElement{
	
    public float size;
    
    public float valStart;
    public float valEnd;
    
    public boolean draggingStart;
    public boolean draggingEnd;
    public boolean draggingAll;
    
    public boolean oldDraggingStart;
    public boolean oldDraggingEnd;
    public boolean oldDraggingAll;
    
    public GuiElement selectionStart;
    public GuiElement selectionEnd;
    
    public int dragX;
    
    public float indexStart;
    public float indexEnd;
    
    public ArrayList<Integer> ages;
    
    public HorizontalScrollBar(ArrayList list) {
    		
    	selectionStart = new GuiElement();
    	selectionStart.backColor = GLOBAL.processing.color(200, 200, 255, 100);

    	selectionEnd = new GuiElement();
    	selectionEnd.backColor = GLOBAL.processing.color(200, 200, 255, 100);
    	
		valEnd = (float)0.957;

		ages = list;
    }

	public void draw()
    {
        int bh = (int)(size * width);
        if(bh < 10) bh = 10;
        
        // Draw selectionStart region
        selectionStart.y = y + 5;
        selectionStart.x = x + (int)(valStart * width);
        selectionStart.width = bh;
        selectionStart.height = height - 5;
        
        // Draw selectionEnd region
        selectionEnd.y = y + 5;
        selectionEnd.x = x + (int)(valEnd * width);
        selectionEnd.width = bh;
        selectionEnd.height = height - 5;
                
        drawVLines();
        
        selectionStart.draw();
        selectionEnd.draw();   
        
        // Ellipse
        GLOBAL.processing.noStroke();
		GLOBAL.processing.fill(GLOBAL.processing.color(180, 180, 255, 40));  
		GLOBAL.processing.ellipse(selectionStart.x + selectionStart.width + (selectionEnd.x - selectionStart.x- selectionStart.width)/2, selectionStart.y+(height-5)/2,10,10);
		GLOBAL.processing.strokeWeight(1);
		GLOBAL.processing.fill(GLOBAL.processing.color(255, 255, 255, 100));
		GLOBAL.processing.stroke(GLOBAL.processing.color(255, 255, 255),200);
		GLOBAL.processing.ellipse(selectionStart.x + selectionStart.width+ (selectionEnd.x - selectionStart.x- selectionStart.width)/2, selectionStart.y+(height-5)/2,10,10);	
		GLOBAL.processing.fill(GLOBAL.processing.color(0, 0, 0, 100));
		GLOBAL.processing.noStroke();
		GLOBAL.processing.ellipse(selectionStart.x+ selectionStart.width + (selectionEnd.x - selectionStart.x- selectionStart.width)/2, selectionStart.y+(height-5)/2,9,9);
       
        // Handle moving all the scrollbar
        if(draggingAll )
        {
            float d = (float)(GLOBAL.processing.mouseX - dragX) / width;
            float tmpV = valStart + d;
            float tmpV2 = valEnd + d;
            if(tmpV >= 0 && tmpV + size <= 1 && tmpV2 >= 0 && tmpV2 + size <= 1)
            {
                valStart = tmpV;
                valEnd = tmpV2;
            }
            dragX = GLOBAL.processing.mouseX;
        }
        
        // Handle start widget dragging.
        else if(draggingStart && GLOBAL.processing.mouseX < selectionEnd.x)
        {
            float d = (float)(GLOBAL.processing.mouseX - dragX) / width;
            float tmpV = valStart + d;
            if(tmpV >= 0 && tmpV + size <= 1)
            {
                valStart = tmpV;
            }
            dragX = GLOBAL.processing.mouseX;
        }
        // Handle start widget dragging.
        else if(draggingEnd  && GLOBAL.processing.mouseX > (selectionStart.x + selectionStart.width))
        {
            float d = (float)(GLOBAL.processing.mouseX - dragX) / width;
            float tmpV = valEnd + d;
            if(tmpV >= 0 && tmpV + size <= 1)
            {
                valEnd = tmpV;
            }
            dragX = GLOBAL.processing.mouseX;
        }

        indexStart = GLOBAL.processing.map(valStart, 0, (float)0.957, 0, ages.size()-1);
        GLOBAL.ageStart = ages.get((int)indexStart).intValue();
//        if (valEnd > 1f)
//        	valEnd = 1f;
        indexEnd = GLOBAL.processing.map(valEnd, 0, (float)0.957, 0, ages.size()-1);
        if (indexEnd > (ages.size() - 1) )
        	indexEnd = (ages.size() - 1);
        GLOBAL.ageEnd = ages.get((int)indexEnd).intValue();
        

    }

	public void mousePressed()
	{

    	if (selectionStart.mouseOver() ) {
    		int bh = (int)(size * width);
    		if(bh < 10) bh = 10;
    		float x1 = x + (valStart * width);
    		float x2 = x1 + bh;
    		if(GLOBAL.processing.mouseX >= x1 && GLOBAL.processing.mouseX <= x2)
    		{
    			draggingStart = true;
    			dragX = GLOBAL.processing.mouseX;
    			return;
    		}
    	}
    	else if (selectionEnd.mouseOver()) {
    		int bh = (int)(size * width);
    		if(bh < 10) bh = 10;
    		float x1 = x + (valEnd * width);
    		float x2 = x1 + bh;
    		if(GLOBAL.processing.mouseX >= x1 && GLOBAL.processing.mouseX <= x2)
    		{
    			draggingEnd = true;
    			dragX = GLOBAL.processing.mouseX;
    			return;
    		}
    	}
    	else if( GLOBAL.processing.mouseX > selectionStart.x + selectionStart.width && GLOBAL.processing.mouseX < selectionEnd.x
    				&& GLOBAL.processing.mouseY > selectionStart.y && GLOBAL.processing.mouseY < selectionStart.y + selectionStart.height) {
    		
    		int bh = (int)(size * width);
    		if(bh < 10) bh = 10;
    		draggingAll = true;
			dragX = GLOBAL.processing.mouseX;
			return;
			
    	}
        
    }
    
    public void drawVLines() {
    	
    	for (int i=0; i< ages.size(); i++) {
    		float x = GLOBAL.processing.map(i, 0, ages.size()-1f, this.x + selectionStart.width/2, this.x + this.width - selectionEnd.width/2);
    		GLOBAL.processing.strokeWeight(2);

		    GLOBAL.processing.stroke(GLOBAL.colorLines);
    		GLOBAL.processing.line(x, y + 12.5f, x, y + height -7.5f);
    	   
    		// Every 5 years
    		if (ages.get(i).intValue() % 5 == 0) {
    			GLOBAL.processing.line(x, y + 7.5f, x, y + height -2.5f);
//    			GLOBAL.processing.fill(GLOBAL.colorText);
//    			GLOBAL.processing.textFont(GLOBAL.tFont,10);
//    			GLOBAL.processing.textAlign(GLOBAL.processing.CENTER);
//    			GLOBAL.processing.text(hours.get(i).intValue(), x, y + 70);
    		}
    	}
		GLOBAL.processing.textAlign(GLOBAL.processing.CENTER);  	
		GLOBAL.processing.fill(GLOBAL.textColor);
		GLOBAL.processing.textFont(GLOBAL.plotFont,12);
    	GLOBAL.processing.text(GLOBAL.ageStart,selectionStart.x + selectionStart.width/2, y+45);
    	GLOBAL.processing.text(GLOBAL.ageEnd,selectionEnd.x + selectionEnd.width/2, y+45);
    
    }
  
    public void mouseReleased()
    {   
    	oldDraggingStart = draggingStart;
    	oldDraggingEnd = draggingEnd;
    	oldDraggingAll = draggingAll;
    	
        draggingStart = false;
        draggingEnd = false;
        draggingAll = false;  

        if(oldDraggingAll!=draggingAll || oldDraggingStart!=draggingStart || oldDraggingEnd!=draggingEnd){
        	GLOBAL.updateFilterResults();
        }

    }
    
}
